﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;

namespace SignalEdgeDetectionWPF
{
    public partial class MainWindow : Window
    {
        private readonly Brush _gridBrush = new SolidColorBrush(Color.FromArgb(255, 220, 220, 220));

        public MainWindow()
        {
            InitializeComponent();
            Loaded += MainWindow_Loaded;
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            if (DataContext is MainViewModel vm) {
                vm.PropertyChanged += Vm_PropertyChanged;
                DrawSignalGraph();
            }
        }

        private void Vm_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == nameof(MainViewModel.Signals)) {
                DrawSignalGraph();
            }
        }

        private void DrawSignalGraph()
        {
            SignalCanvas.Children.Clear();

            // 绘制网格
            DrawGrid();

            if (!(DataContext is MainViewModel vm)) return;

            // 绘制信号
            int yOffset = 30;
            int heightPerSignal = 30;

            for (int i = 0; i < vm.Signals.Count; i++) {
                var signal = vm.Signals[i];
                int yPos = yOffset + i * heightPerSignal;

                // 绘制信号名称
                var nameText = new TextBlock {
                    Text = $"{signal.Name} ({signal.Pin}):",
                    FontFamily = new FontFamily("Consolas"),
                    FontSize = 9,
                    Foreground = Brushes.Black,
                    Margin = new Thickness(10, yPos, 0, 0)
                };
                SignalCanvas.Children.Add(nameText);

                // 绘制信号类型
                var typeText = new TextBlock {
                    Text = signal.SignalType,
                    FontFamily = new FontFamily("Consolas"),
                    FontSize = 9,
                    Foreground = signal.SignalTypeColor,
                    Margin = new Thickness(150, yPos, 0, 0)
                };
                SignalCanvas.Children.Add(typeText);

                // 绘制信号状态
                var stateEllipse = new Ellipse {
                    Width = 12,
                    Height = 12,
                    Fill = signal.StateColor,
                    Margin = new Thickness(200, yPos, 0, 0)
                };
                SignalCanvas.Children.Add(stateEllipse);

                // 绘制触发指示器
                if ((DateTime.Now - signal.LastTriggerTime).TotalSeconds < 1) {
                    var triggerRect = new Rectangle {
                        Width = 100,
                        Height = 12,
                        Fill = Brushes.Orange,
                        Margin = new Thickness(220, yPos, 0, 0)
                    };
                    SignalCanvas.Children.Add(triggerRect);

                    var triggerText = new TextBlock {
                        Text = "触发!",
                        FontFamily = new FontFamily("Consolas"),
                        FontSize = 8,
                        FontWeight = FontWeights.Bold,
                        Foreground = Brushes.DarkRed,
                        Margin = new Thickness(225, yPos, 0, 0)
                    };
                    SignalCanvas.Children.Add(triggerText);
                }
            }
        }

        private void DrawGrid()
        {
            // 水平线
            for (int i = 0; i < SignalCanvas.ActualHeight; i += 20) {
                var line = new Line {
                    X1 = 0,
                    Y1 = i,
                    X2 = SignalCanvas.ActualWidth,
                    Y2 = i,
                    Stroke = _gridBrush,
                    StrokeThickness = 1
                };
                SignalCanvas.Children.Add(line);
            }

            // 垂直线
            for (int i = 0; i < SignalCanvas.ActualWidth; i += 20) {
                var line = new Line {
                    X1 = i,
                    Y1 = 0,
                    X2 = i,
                    Y2 = SignalCanvas.ActualHeight,
                    Stroke = _gridBrush,
                    StrokeThickness = 1
                };
                SignalCanvas.Children.Add(line);
            }
        }
    }
}